#include "Headers/server.h"

server::server()
{
    is_viod=false;
    StartTcpPort=1111;
    StartTcpServer = new QTcpServer();
    StartTcpServer->listen(QHostAddress::Any,StartTcpPort);
    MessagesTcpPort=1112;
    MessagesTcpServer = new QTcpServer();
    MessagesTcpServer->listen(QHostAddress::Any,MessagesTcpPort);
    GetVidoTcpPort=1114;
    GetVidoTcpServer = new QTcpServer();
    GetVidoTcpServer->listen(QHostAddress::Any,GetVidoTcpPort);
    SetVidoTcpPort=1115;
    SetVidoTcpServer = new QTcpServer();
    SetVidoTcpServer->listen(QHostAddress::Any,SetVidoTcpPort);
}

void server::MessagesTcp(){
    connect(MessagesTcpServer, &QTcpServer::newConnection, this,[=](){
        QTcpSocket* socket = MessagesTcpServer->nextPendingConnection();
        connect(socket, &QTcpSocket::readyRead, [=]() {
            QString data = socket->readAll();
            QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toUtf8());
            QVariant variant = jsonDocument.toVariant();
            QMap<QString, QVariant> map = variant.toMap();
            QString User = map["User"].toString();
            int Type = map["Type"].toInt();
            if(Type==JOIN_TYPE){
                MessagesTcpList[User]=socket;
                return ;
            }
            if(MessagesTcpList[User]!=nullptr&&Type==SEND_TYPE){
                MessagesTcpList[User]->write(data.toUtf8());
            }else{
                //SQL存储，等待用户上线再发送
                return ;
            }
        });
    });
}

void server::GetVidoUdp(){
    connect(GetVidoTcpServer, &QTcpServer::newConnection, this,[=](){
        QTcpSocket* socket = GetVidoTcpServer->nextPendingConnection();
        connect(socket, &QTcpSocket::readyRead, [=]() {
            if(is_viod){
                QByteArray data = socket->readAll();
                qDebug()<<sendVido[socket];
                sendVido[socket]->write(data);
                if(data=="Commopasd")is_viod=false;
                return;
            }
            QString data = socket->readAll();
            QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toUtf8());
            QVariant variant = jsonDocument.toVariant();
            QMap<QString, QVariant> map = variant.toMap();
            QString User = map["User"].toString();
            QString Sender = map["Sender"].toString();
            int Type = map["Type"].toInt();
            if(Type==JOIN_TYPE){
                GetVidoTcpList[User]=socket;
            }else if(Type==SEND_TYPE){
                sendVido[GetVidoTcpList[Sender]]=SetVidoTcpList[User];
                sendVido[GetVidoTcpList[User]]=SetVidoTcpList[Sender];
                GetVidoTcpList[User]->write(data.toUtf8());
            }else if(Type==VOID_OK_TYPE){
                GetVidoTcpList[User]->write(data.toUtf8());
                is_viod = true;
            }
            else if(Type==VOID_NO_TYPE){
                //SQL存储，等待用户上线再发送
                is_viod = false;
                GetVidoTcpList[User]->write(data.toUtf8());
            }else{
                return ;
            }
        });
    });
}

void server::SetVidoUdp(){
    connect(SetVidoTcpServer, &QTcpServer::newConnection, this,[=](){
        QTcpSocket* socket = SetVidoTcpServer->nextPendingConnection();
        connect(socket, &QTcpSocket::readyRead, [=]() {
            QString data = socket->readAll();
            QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toUtf8());
            QVariant variant = jsonDocument.toVariant();
            QMap<QString, QVariant> map = variant.toMap();
            QString User = map["User"].toString();
            QString Sender = map["Sender"].toString();
            int Type = map["Type"].toInt();
            if(Type==JOIN_TYPE){
                SetVidoTcpList[User]=socket;
            }else{
                //SQL存储，等待用户上线再发送
                return ;
            }
        });
    });
}

void server::start(){
    MessagesTcp();
    GetVidoUdp();
    SetVidoUdp();
    fileThread = new FileThread();
    fileThread->start();
}
